<template>
    <div class="pushRule zy-content ">
        <!-- 搜索 -->
        <div class="searchBox search-box-layout">
            <Row :gutter="24">
                <Col span="4">
                <div>
                    <p class="label">{{ $t('配置方案名称') }}</p>
                    <div class="ipt">
                        <Input type="text" v-model="searchParams.configName"
                            :placeholder="`${$t('common.inputInfo')}`"></Input>
                    </div>
                </div>
                </Col>
                <Col span="4">
                <div>
                    <p class="label">{{ $t('级别') }}</p>
                    <div class="ipt">
                        <Select v-model="searchParams.maxLevel" clearable :placeholder="`${$t('common.selectInfo')}`">
                            <Option v-for="(item, index) of levelList" :value="item" :key="index">{{ item }}</Option>
                        </Select>
                    </div>
                </div>
                </Col>
                <Col span="4">
                <div>
                    <p class="label">{{ $t('操作员') }}</p>
                    <div class="ipt">
                        <Select v-model="searchParams.operator" filterable clearable
                            :placeholder="`${$t('common.selectInfo')}`" @on-query-change="timeOutgetUserNo">
                            <Option v-for="(item, index) of userNoArr" :value="item.realname" :key="index">{{
                                item.realname }}</Option>
                        </Select>
                    </div>
                </div>
                </Col>
                <!-- <Col span="4">
                <div>
                    <p class="label">{{ $t('推送类型') }}</p>
                    <div class="ipt">
                        <Select v-model="searchParams.pushType" clearable :placeholder="`${$t('common.selectInfo')}`">
                            <Option v-for="(item, index) of pushTypeList" :value="item.id" :key="index">{{ item.name }}
                            </Option>
                        </Select>
                    </div>
                </div>
                </Col> -->
                <Col span="4">
                <div>
                    <p class="label hide-label">--</p>
                    <div class="ipt">
                        <Button class="search-btn" type="primary" @click="changeNum()">{{ $t('common.Query') }}</Button>
                        <Button class="reset-btn" @click="reset()">{{ $t('common.reset') }}
                        </Button>
                    </div>
                </div>
                </Col>
            </Row>
        </div>
        <div class="list-layout mt16">
            <div class="btns clear">
                <span class="title">{{ $t('common.listInformation') }}</span>
                <Button @click="createfaultCodeShowH">{{ $t('common.New') }}</Button>
                <Button @click="delFaultCodeShowClick">{{ $t('common.batchDelete') }}</Button>
                <!-- <Button @click="exportTemplateClick">{{ $t('common.batchExport') }}</Button> -->
                <Button @click="getTableColumns(columns)">
                    {{ handleContextMenuStatus ? $t('显示操作栏') : $t('隐藏操作栏') }}
                </Button>
            </div>
            <!-- 表格 -->
            <div class="tableContent">
                <Table ellipsis ref="selection" stripe class="resizable-table" border
                    @on-column-width-resize="onColumnWidthResize" :columns="columnsList" :data="dataArr"
                    @on-selection-change="onSelectChange">

                    <template slot-scope="{ row }" slot="action">
                        <span class="operationBtn" @click="createfaultCodeShowH(row, '查看')"><i
                                class="iconfont zy-common-chakan"></i>{{ $t('查看') }}</span>
                        <span class="operationBtn" @click="createfaultCodeShowH(row)"><i
                                class="iconfont zy-common-bianji"></i>{{ $t('common.Edit') }}</span>
                        <span class="operationBtn" v-if="row.status === 0" @click="disabled(row)">
                            <i class="iconfont zy-common-bianji"></i>
                            {{ $t('启用') }}
                        </span>
                        <span class="operationBtn" v-if="row.status === 1" @click="disabled(row)">
                            <i class="iconfont zy-common-bianji"></i>
                            {{ $t('common.disable') }}
                        </span>
                        <span class="operationBtn" @click="delPrrShowH(row)"><i class="iconfont zy-common-shanchu"></i>{{
                            $t('common.Delete') }}</span>
                    </template>
                </Table>
            </div>
            <!-- 分页 -->
            <div class="pageWrap mt16">
                <pageLeftTitle :total="total" :selected="delIds.length" />
                <Page :total="total" :page-size="pageSize" :page-size-opts="$variable.pageSizes" :current="pageNum"
                    @on-change="changePageNum" @on-page-size-change="changePageSize" show-sizer show-total />
            </div>
        </div>
        <!-- 新增编辑 -->
        <Modal :mask-closable="false" v-model="create" :title="addtitle" class="modal-content-item-layout" width="1200">
            <!-- {{ formItem }} -->
            <div class="addProject addPushRoleWrap">
                <Form ref="form" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
                    <div class="content-item">
                        <span class="content-item-title">{{ $t('推送规则') }}</span>
                        <div class="pd16">
                            <Row :gutter="20">
                                <Col span="6">
                                <FormItem :label="$t('类别')" prop="category">
                                    <Select v-model="formItem.category" clearable :disabled="showF"
                                        :placeholder="`${$t('common.selectInfo')}`">
                                        <Option v-for="(item, index) of categoryList" :value="item.name" :key="index">{{
                                            item.name }}</Option>
                                    </Select>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('配置方案名称')" prop="configName">
                                    <Input type="text" v-model="formItem.configName" :disabled="showF"
                                        :placeholder="`${$t('common.inputInfo')}`"></Input>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('问题类型')" prop="eventType">
                                    <Select v-model="formItem.eventType" clearable :disabled="showF"
                                        :placeholder="`${$t('common.selectInfo')}`">
                                        <Option v-for="(item, index) of eventTypeList" :value="item.name" :key="index">{{
                                            item.name }}</Option>
                                    </Select>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('最大升级级别')" prop="maxLevel">
                                    <Select v-model="formItem.maxLevel" clearable :disabled="showF"
                                        :placeholder="`${$t('common.selectInfo')}`" @on-change="getLevelList">
                                        <Option v-for="(item, index) of levelList" :value="item" :key="index">{{ item }}
                                        </Option>
                                    </Select>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('工站')" prop="factory1">
                                    <Cascader :disabled="showF" clearable :data="orgList" v-model="formItem.factory1"
                                        :placeholder="$t('common.PleaseSelect')" change-on-select></Cascader>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('操作人')" prop="operatorNo">
                                    <Select v-if="showF"  :disabled="true" v-model="formItem.operatorNo" filterable clearable
                                        @on-query-change="timeOutgetUserNo2">
                                        <Option v-for="(item, index) of userNoArr2" :value="item.empno" :key="index">{{
                                            item.realname }}</Option>
                                    </Select>
                                    <Input v-else type="text" v-model="formItem.operator" disabled></Input>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('描述')">
                                    <Input :disabled="showF" type="text" v-model="formItem.description"
                                        :placeholder="`${$t('common.inputInfo')}`"></Input>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('升级选项')">
                                    <CheckboxGroup v-model="formItem.handlingUpgrades">
                                        <Checkbox :disabled="showF" :label="item.value" v-for="item in upgradeItems"
                                            :key="item.value">
                                            {{ item.label }}
                                        </Checkbox>
                                    </CheckboxGroup>
                                </FormItem>
                                </Col>
                                <Col span="8">
                                <FormItem :label="$t('fault.ImpactProduction')">
                                    <CheckboxGroup v-model="formItem.reasonForScrapping" :disabled="showF">
                                        <Checkbox :label="item.value" :disabled="showF" v-for="item in effects"
                                            :key="item.value">
                                            {{ item.label }}
                                        </Checkbox>
                                    </CheckboxGroup>
                                </FormItem>
                                </Col>
                            </Row>
                        </div>
                    </div>
                    <div class="content-item" v-for="(obj, index) of pushRuleLevelVoList" :key="index">
                        <span class="content-item-title">{{ `${$t('级别')}${index+1}${$t('升级')}` }}</span>
                        <div class="pd16">
                            <Row :gutter="20">
                                <Col span="6">
                                <FormItem :label="$t('用户')" prop="userList">
                                    <div @click="selectUser(obj.userId, obj.userName, obj.level)">
                                        <Select v-model="obj.userName" :disabled="true" :max-tag-count="1"
                                            :max-tag-placeholder="maxTagPlaceholder" multiple
                                            :placeholder="`${$t('common.selectInfo')}`">
                                            <Option v-for="(item, index) of obj.userName" :value="item" :key="index">
                                                {{ item }}</Option>
                                        </Select>
                                    </div>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('响应过期时间')" prop="responseTimeoutTime">
                                    <Input type="number" v-model="obj.responseTimeoutTime" :disabled="showF"
                                        :placeholder="`${$t('common.inputInfo')}`">
                                    <span slot="append">s</span>
                                    </Input>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('处理过期时间(s)')" prop="repairTimeoutTime">
                                    <InputNumber style="width: 100%" v-model="obj.repairTimeoutTime" :disabled="showF"
                                        :placeholder="`${$t('common.inputInfo')}`">
                                        <!-- <span slot="append">s</span> -->
                                    </InputNumber>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('推送类型')" prop="pushType">
                                    <CheckboxGroup v-model="obj.enableEmail">
                                        <Checkbox :label="1" :disabled="showF">{{ $t('电子邮件') }}</Checkbox>
                                        <!-- <Checkbox label="2" :disabled="showF">{{$t('智能手表')}}</Checkbox> -->
                                        <!-- <Checkbox label="3" :disabled="showF">{{$t('站内消息')}}</Checkbox> -->
                                    </CheckboxGroup>
                                </FormItem>
                                </Col>
                                <Col span="6">
                                <FormItem :label="$t('描述')" prop="description">
                                    <Input v-model="obj.description" :disabled="showF"
                                        :placeholder="`${$t('common.inputInfo')}`"></Input>
                                </FormItem>
                                </Col>
                            </Row>
                        </div>
                    </div>
                </Form>
            </div>
            <div slot="footer">
                <Button type="default" @click="create = false">{{ $t('common.Cancle') }}</Button>
                <Button type="primary" :loading="loading" @click="addOk('form')" v-if="!showF">{{
                    $t('maintenance.Confirmed') }}</Button>
            </div>
        </Modal>
        <Modal :mask-closable="false" v-model="userShowF" :title="$t('选择用户')" width="1200">
            <Row :gutter="20">
                <Form class="formList" label-position="top">
                    <Col span="6">
                    <FormItem :label="$t('部门')">
                        <Select v-model="deptId" :disabled="showF" filterable clearable
                            :placeholder="`${$t('common.selectInfo')}`" @on-change="timeOutgetUserNo1(userKey)">
                            <Option v-for="(item, index) of departmentList" :value="item.id" :key="index">{{ item.deptName
                            }}
                            </Option>
                        </Select>
                    </FormItem>
                    </Col>
                    <Col span="6">
                    <FormItem :label="$t('角色')">
                        <Select v-model="roleId" :disabled="showF" filterable clearable
                            :placeholder="`${$t('common.selectInfo')}`" @on-change="timeOutgetUserNo1(userKey)">
                            <Option v-for="(item, index) of roleList" :value="item.id" :key="index">{{ item.name }}</Option>
                        </Select>
                    </FormItem>
                    </Col>
                    <Col span="6">
                    <FormItem :label="$t('用户')">
                        <div @click="userShowF = true">
                            <Input v-model="userKey" :disabled="showF" :placeholder="`${$t('common.inputInfo')}`" clearable
                                @on-change="timeOutgetUserNo1(userKey)" />
                        </div>
                    </FormItem>
                    </Col>
                </Form>
            </Row>
            <Row>
                <Col span="12" align="center">
                <Table ellipsis ref="selection" :columns="userColumns" :data="leftList" height="520" width="580">
                    <template slot-scope="{ row }" slot="action">
                        <span style="color: #3271f0;cursor: pointer;" @click="selectClick(row)">{{ $t('选择') }}</span>
                    </template>
                </Table>
                </Col>
                <Col span="12" align="center">
                <Table ellipsis ref="selection" :columns="userColumns" :data="rightList" height="520" width="580">
                    <template slot-scope="{ row }" slot="action">
                        <!-- {{ row }} -->
                        <span style="color: #3271f0;cursor: pointer;" class="operationBtn" @click="delClick(row)"
                            v-if="!showF">
                            {{ $t('common.Delete') }}
                        </span>
                    </template>
                </Table>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="default" @click="userShowF = false">{{ $t('common.Cancle') }}</Button>
                <Button type="primary" :loading="loading" @click="addConfirm" v-if="!showF">{{ $t('maintenance.Confirmed')
                }}</Button>
            </div>
        </Modal>
        <!-- 请确认是否删除弹框 -->
        <Modal :mask-closable="false" v-model="delFaultCodeShowF" :title="$t('common.Delete')" class="modal-title-layout">
            <div class="icon red">
                <i class="iconfont zy-common-shanchu"></i>
            </div>
            <p class="title">
                <span>{{ $t('common.confirmDelete') }}</span>
            </p>
            <div slot="footer">
                <Button type="default" @click="delFaultCodeShowF = false">{{ $t('common.Cancle') }}</Button>
                <Button type="primary" :loading="loading" @click="deleteOk">{{ $t('maintenance.Confirmed') }}</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import { addApi, deleteApi, disabled, searchApi, updateApi } from "@/api/maintenance/deviceRepairRule.js";
import toggleTableOperation from "@/mixins/toggleTableOperation";
import recursion from "@/util/recursion";
export default {
    name: "pushRule",//仓库配置
    components: {},
    mixins: [toggleTableOperation],
    data() {
        return {
            effects: [
                { label: "影响生产停机", value: '影响生产停机' },
                { label: "影响生产未停机", value: '影响生产未停机' },
                { label: "不影响生产", value: '不影响生产' },
            ],
            upgradeItems: [
                { label: "响应升级", value: 1 },
                { label: "处理升级", value: 2 },
                // {label: "复核升级", value: 2},
            ],
            userShowF: false,
            userKey: '',
            userColumns: [
                {
                    title: `${this.$t('工号')}`,
                    key: 'empno',
                    align: 'center'
                },
                {
                    // title: '用户名称',
                    title: `${this.$t('用户名称')}`,
                    key: 'realname',
                    align: 'center'
                },
                {
                    title: `${this.$t('common.operation')}`,
                    slot: "action",
                    align: "center"
                }
            ],
            leftList: [],
            rightList: [],
            orgList: [],
            userNoArr: [],
            categoryList: [
                // {id:1,name:`${ this.$t('其他') }`},
                { id: 2, name: '设备维修' },
            ],
            levelList: [1, 2, 3, 4, 5],
            pushTypeList: [
                { id: 1, name: `${this.$t('电子邮件')}` },
                // { id: 2, name: `${this.$t('智能手表')}` },
                // { id: 3, name: `${this.$t('站内消息')}` },
            ],
            eventTypeList: [
                { id: 1, name: '设备问题' },
                { id: 2, name: '质量问题' },
                { id: 3, name: '物料问题' },
                { id: 4, name: '人为问题' },
                { id: 5, name: '其他问题' },
            ],
            departmentList: [],
            roleList: [],
            formItem: {},
            addtitle: "",
            allCheck: false,
            loading: false,
            create: false,
            searchParams: {},
            pageNum: 1,
            pageSize: 10,
            total: 0,
            columns: [
                {
                    key: 'selection',
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: `${this.$t('规则类别')}`,
                    key: 'category',
                    minWidth: 130,
                    align: 'center',
                    resizable: true
                },
                {
                    title: `${this.$t('配置方案名称')}`,
                    key: 'configName',
                    minWidth: 180,
                    align: 'center',
                    resizable: true
                },
                {
                    title: `${this.$t('问题类型')}`,
                    key: 'eventType',
                    minWidth: 120,
                    align: 'center',
                    resizable: true
                },
                {
                    title: `${this.$t('级别')}`,
                    key: 'maxLevel',
                    minWidth: 100,
                    align: 'center',
                    resizable: true,
                    tooltip: true
                },
                {
                    title: `${this.$t('工站')}`,
                    key: 'factory',
                    minWidth: 180,
                    align: 'center',
                    resizable: true,
                    tooltip: true
                },
                {
                    title: `${this.$t('操作人')}`,
                    key: 'operator',
                    minWidth: 100,
                    align: 'center',
                    resizable: true
                },
                {
                    title: `${this.$t('状态')}`,
                    key: 'status',
                    minWidth: 100,
                    align: 'center',
                    resizable: true,
                    render: (h, params) => {
                        return h('div', params.row.status == 1 ? '启用' : '禁用')
                    },
                },
                {
                    title: `${this.$t('最新更新时间')}`,
                    key: 'updateTime',
                    minWidth: 180,
                    align: 'center',
                    resizable: true
                },
                {
                    title: `${this.$t('最新更新人')}`,
                    key: 'updateOperator',
                    minWidth: 140,
                    align: 'center',
                    resizable: true
                },
                {
                    //title: '操作',
                    title: `${this.$t('common.operation')}`,
                    slot: "action",
                    fixed: 'right',
                    minWidth: 360,
                    align: "center"
                }
            ],
            dataArr: [],
            delFaultCodeShowF: false,//是否显示删除弹窗
            pageSizeOption: [10, 20, 30, 40],
            delIds: [],
            selection: [],
            ruleValidate: {
                category: [{ required: true, message: this.$t('common.selectInfo'), trigger: 'change' }],
                configName: [{ required: true, message: this.$t('common.inputInfo'), trigger: 'blur' }],
                eventType: [{ required: true, message: this.$t('common.selectInfo'), trigger: 'change' }],
                maxLevel: [{ required: true, type: 'number', message: this.$t('common.selectInfo'), trigger: 'change' }],
                factory1: [{ required: true, message: this.$t('common.selectInfo'), trigger: 'change', type: 'array' }],
            },
            userArr: [],
            pushRuleLevelVoList: [],
            userNoArr2: [],
            deptId: '',
            roleId: '',
            level: '',
            showF: false,
        }
    },
    created() {
        this.getTableColumnsWidth(this.columns)
    },
    mounted() {
        this.searchList();
        this.getDept()
        this.getRole()
        this.getOrgList()
        this.getUserNo(null, 1);
        this.timeOutgetUserNo2()
        // this.changeState();
    },
    methods: {
        addConfirm() {
            this.loading = true
            this.pushRuleLevelVoList.some((item, i) => {
                if (item.level === this.level) {
                    item.userName = this.rightList.map((obj) => {
                        return obj.realname;
                    });
                    item.userId = this.rightList.map((obj) => {
                        return obj.id;
                    });
                    return true
                }
            })
            this.loading = false
            this.userShowF = false
        },
        selectUser(ids, names, level) {
            this.getUserNo1()
            this.userShowF = true
            this.leftList = []
            this.deptId = ''
            this.roleId = ''
            this.userKey = ''
            this.rightList = []
            for (let index = 0; index < ids.length; index++) {
                this.rightList.push({
                    realname: names[index],
                    id: ids[index]
                })
            }
            this.level = level
        },
        selectClick(row) {
            let status = false
            this.rightList.some((item, i) => {
                if (item.id === row.id) {
                    this.$Message.warning(({ content: this.$t("该用户已被选择"), duration: 6, closable: true }));
                    status = true
                }
            })
            if (status === false) {
                this.rightList.unshift(row)
            }
        },
        delClick(row) {
            this.rightList.some((item, i) => {
                if (item.id === row.id) {
                    this.rightList.splice(i, 1)
                    return true
                }
            })
        },
        // 获取使用位置
        getOrgList() {
            this.axios({
                method: "get",
                url: "/basic/organization/findListByToken",
                params: { level: 0 },
            }).then((res) => {
                if (res.code === 200) {
                    const obj = {
                        label: "name",
                        value: "code",
                        children: "children",
                    };
                    this.orgList = recursion.convertTree(res.data, obj);
                } else {
                    this.$Message.warning({
                        content: res.msg,
                        duration: 6,
                        closable: true,
                    });
                }
            });
        },
        changeDetSelect(nodes, id, values) {
            for (let i = 0; i < nodes.length; i++) {
                const node = nodes[i];
                if (node.value === id) {
                    values.push(node.value);
                    return true;
                }
                if (Array.isArray(node.children)) {
                    if (this.changeDetSelect(node.children, id, values)) {
                        values.push(node.value);
                        return true;
                    }
                }
            }
            return false;
        },
        maxTagPlaceholder(num) {
            return '...' + num;
        },
        getLevelList(query) {
            let arr = JSON.parse(JSON.stringify(this.pushRuleLevelVoList))
            if (query > arr.length) {
                for (let i = arr.length; i < query; i++) {
                    arr.push({ level: i + 1, userName: [], userId: [] })
                }
            } else {
                for (let i = arr.length; i > query; i--) {
                    arr.pop()
                }
            }
            this.pushRuleLevelVoList = arr
        },
        getDept() {
            let data = {
                deptName: '',
                pageNo: 1,
                pageSize: 1000,
            };
            this.axios({
                method: 'post',
                url: '/basic/dept/list',
                data
            }).then(res => {
                if (res.code === 200) {
                    this.departmentList = res.data.list
                }
            })
        },
        getRole() {
            let params = {
                pageNum: 1,
                pageSize: 100,
            };
            this.axios({
                method: 'get',
                url: '/basic/role/page',
                params
            }).then(res => {
                if (res.code === 200) {
                    this.roleList = res.data.list
                }
            })
        },
        //延时查询
        timeOutgetUserNo(query) {
            clearTimeout(this.timeout);
            let isSearchF = false;
            this.timeout = setTimeout(() => {
                if (query != "") {
                    for (let item of this.userNoArr) {
                        if (item.empno == query || item.empno + '-' + item.realname == query || item.realname == query) {
                            isSearchF = true
                        }
                    }
                    if (isSearchF == false) {
                        this.getUserNo(query, 1);
                    }
                }
            }, 500);
        },
        //延时查询
        timeOutgetUserNo2(query) {
            clearTimeout(this.timeout);
            let isSearchF = false;
            this.timeout = setTimeout(() => {
                if (query != "") {
                    for (let item of this.userNoArr2) {
                        if (item.empno == query || item.empno + '-' + item.realname == query || item.realname == query) {
                            isSearchF = true
                        }
                    }
                    if (isSearchF == false) {
                        this.getUserNo(query, 2);
                    }
                }
            }, 500);
        },
        getUserNo(query, type) {
            return new Promise(resolve => {
                this.axios({
                    method: 'get',
                    url: '/basic/user/searchUser',
                    params: {
                        key: query
                    }
                }).then(res => {
                    if (res.code === 200) {
                        if (type === 1) {
                            this.userNoArr = res.data
                        } else {
                            this.userNoArr2 = res.data
                        }
                    } else {
                        this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                    }
                }).finally(() => {
                    resolve()
                })
            });
        },
        //延时查询
        timeOutgetUserNo1(query) {
            clearTimeout(this.timeout);
            this.timeout = setTimeout(() => {
                this.getUserNo1(query);
            }, 500);
        },
        updatedeptrole(item) {
            this.roleId = item.roleId
            this.deptId = item.deptId
            this.userId = []
            this.userName = []

        },
        getUserNo1(query) {
            return new Promise(resolve => {
                this.axios({
                    method: 'get',
                    url: '/basic/user/findUserDetail',
                    params: {
                        deptId: this.deptId,
                        roleId: this.roleId,
                        empno: query
                    }
                }).then(res => {
                    if (res.code === 200) {
                        this.leftList = res.data
                    } else {
                        this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                    }
                }).finally(() => {
                    resolve()
                })
            });
        },
        reset() {
            this.searchParams = {};
            this.changeNum();
        },
        //分页
        changeNum() {
            this.pageNum = 1;
            this.searchList();
        },
        changePageSize(pageSize) {
            this.pageSize = pageSize;
            this.searchList();
        },
        changePageNum(val) {
            this.pageNum = val;
            this.searchList();
        },
        //查询列表
        searchList() {
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
            };
            for (let key in this.searchParams) {
                if (this.searchParams[key]) {
                    this.$set(params, key, this.searchParams[key])
                }
            }
            searchApi(params).then(res => {
                if (res.code === 200) {
                    this.dataArr = res.data.records
                    this.total = res.data.total
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        disabled(row) {
            let params = {
                id: row.id,
            }
            disabled(params, row.status == 1 ? true : false).then(res => {
                if (res.code === 200) {
                    this.changeNum();
                    this.$Message.success(res.msg);
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        //增加或编辑
        async createfaultCodeShowH(rowData, type) {
            const row = JSON.parse(JSON.stringify(rowData))
            this.$refs.form.resetFields();
            if (row.id) {
                if (type === '查看') {
                    this.addtitle = `${this.$t('查看')}`
                    this.showF = true
                } else {
                    this.addtitle = `${this.$t('common.Edit')}`
                    this.showF = false
                }
                this.formItem = {
                    id: row.id,
                    operator: row.operator,
                    operatorNo: row.operatorNo - 0,
                    category: row.category,
                    configName: row.configName,
                    description: row.description,
                    eventType: row.eventType,
                    factory1: row.factory1 || [],
                    maxLevel: row.maxLevel,
                    reasonForScrapping: row.reasonForScrapping || [],
                    handlingUpgrades: row.handlingUpgrades,
                }
                this.pushRuleLevelVoList = row.levels
                // await this.getDetail(row.id)
                // await this.getUserNo(row.operatorNo, 2)
                // let _affectProductionFlags = row.affectProductionFlags ? row.affectProductionFlags.split(",") : [];

                // if (Array.isArray(_affectProductionFlags)) {
                //     for (let i = 0; i < _affectProductionFlags.length; i++) {
                //         _affectProductionFlags[i] = _affectProductionFlags[i] / 1;
                //     }
                // }

                // let _upgradeItems = row.upgradeItems ? row.upgradeItems.split(",") : [];

                // if (Array.isArray(_upgradeItems)) {
                //     for (let i = 0; i < _upgradeItems.length; i++) {
                //         _upgradeItems[i] = _upgradeItems[i] / 1;
                //     }
                // }

                // this.formItem = {
                //     id: row.id,
                //     category: row.category,
                //     configName: row.configName,
                //     description: row.description,
                //     eventType: row.eventType,
                //     maxLevel: row.maxLevel,
                //     affectProductionFlags: _affectProductionFlags,
                //     // upgradeItems: _upgradeItems,
                //     operatorNo: row.operatorNo,
                //     factory1: [],
                // }
                // this.changeDetSelect(this.orgList, row.factory1, this.formItem.factory1)
                // this.formItem.factory1.reverse()
            } else {
                this.showF = false
                this.addtitle = `${this.$t('common.New')}`
                this.formItem = {
                    operator: window.localStorage.getItem('ziyunFactoryPortalName'),
                    operatorNo: window.localStorage.getItem('ziyunFactoryUserId') - 0,
                };
                this.pushRuleLevelVoList = []
            }
            this.create = true;
        },
        // getDetail(id) {
        //     let params = { id }
        //     return new Promise(resolve => {
        //         detailApi(params).then(res => {
        //             if (res.code === 200) {
        //                 this.userArr = []
        //                 for (let item of res.data) {
        //                     item.pushType = item.pushType.split(',')
        //                     item.userArr = [...item.userList]
        //                     item.userList = item.userList.map((obj) => {
        //                         return obj.empno;
        //                     });
        //                 }
        //                 this.pushRuleLevelVoList = res.data
        //             } else {
        //                 this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
        //             }
        //         }).finally(() => {
        //             resolve()
        //         })
        //     });
        // },
        addOk(name) {
            this.$refs[name].validate((valid) => {
                if (!valid) {
                    return
                } else {
                    let str = /^(\-|\+)?\d+(\.\d+)?$/
                    let status = false
                    // this.pushRuleLevelVoList.some((item, i) => {
                    //     if (!(item.userList && item.userList.length > 0)) {
                    //         this.$Message.warning(({ content: `${this.$t('common.selectInfo')} ${this.$t('级别')}${item.level}${this.$t('升级')} ${this.$t('用户')}`, duration: 6, closable: true }));
                    //         status = true
                    //         return true
                    //     }
                    //     if (!(item.pushType && item.pushType.length > 0)) {
                    //         this.$Message.warning(({ content: `${this.$t('common.selectInfo')} ${this.$t('级别')}${item.level}${this.$t('升级')} ${this.$t('推送类型')}`, duration: 6, closable: true }));
                    //         status = true
                    //         return true
                    //     }
                    //     if (!(str.test(item.responseTimeoutTime))) {
                    //         this.$Message.warning(({ content: `${this.$t('common.inputInfo')} ${this.$t('级别')}${item.level}${this.$t('升级')} ${this.$t('响应过期时间')}`, duration: 6, closable: true }));
                    //         status = true
                    //         return true
                    //     }
                    // })

                    if (status) {
                        return false
                    }
                    let params = { ...this.formItem, levels: JSON.parse(JSON.stringify(this.pushRuleLevelVoList)) }
                    // params.factory1 = params.factory1.at(-1)

                    // for (let item of params.levels) {
                    //     // if (this.formItem.id) {
                    //     //     item.ruleEventId = this.formItem.id
                    //     // }
                    //     item.userName = item.userArr.map((obj) => {
                    //         return obj.realname;
                    //     });
                    //     item.userId = item.userList.map((obj) => {
                    //         return obj.id;
                    //     });
                    // }
                    console.log(params, 123123);
                    // params['affectProductionFlags'] = params.affectProductionFlags.join(',');
                    // params['upgradeItems'] = params.upgradeItems.join(',');
                    this.loading = true;
                    if (this.formItem.id) {
                        updateApi(params).then(res => {
                            this.loading = false;
                            if (res.code === 200) {
                                this.changeNum();
                                this.$Message.success(res.msg);
                                this.create = false;
                            } else {
                                this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                            }
                        })
                    } else {
                        addApi(params).then(res => {
                            this.loading = false;
                            if (res.code === 200) {
                                this.changeNum();
                                this.$Message.success(res.msg);
                                this.create = false;
                            } else {
                                this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                            }
                        })
                    }
                }
            })
        },
        //勾选列表
        onSelectChange(selection) {
            let myArray = [];
            selection.map((item, index) => {
                myArray.push(item.id);
            });
            this.delIds = myArray;
            if (selection.length > 0) {
                this.allCheck = true;
            } else {
                this.allCheck = false;
            }
        },
        //批量删除
        delFaultCodeShowClick() {
            if (this.delIds.length == 0) {
                this.$Message.warning({
                    content: `${this.$t('common.deleteItems')}`,
                    duration: 6,
                    closable: true,
                });
                return;
            } else {
                this.delFaultCodeShowF = true;
            }
        },
        //删除
        delPrrShowH(row) {
            //点击展示提示删除弹窗
            this.delFaultCodeShowF = true;
            this.delIds = [];
            this.delIds.push(row.id);
        },
        deleteOk() {
            this.loading = true;
            deleteApi(this.delIds).then(res => {
                this.loading = false;
                if (res.code === 200) {
                    this.changeNum();
                    this.$Message.success(res.msg);
                    this.delIds = [];
                    this.allCheck = false;
                    this.delFaultCodeShowF = false;
                } else {
                    this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
                }
            })
        },
        //导出
        // exportTemplateClick() {
        //     let params = {};
        //     for (let key in this.searchParams) {
        //         if (this.searchParams[key]) {
        //             this.$set(params, key, this.searchParams[key])
        //         }
        //     }
        //     const filName = `${this.$t('推送规则列表')}`
        //     exportApi(params).then(res => {
        //         fileDownload(res, filName + '.xlsx')
        //     })
        // }
    }
}
</script>

<style lang="scss" scoped>
::v-deep.ivu-select-input[disabled] {
    -webkit-text-fill-color: #515a6e
}

::v-deep.ivu-select-multiple .ivu-tag {
    max-width: 70%
}

.addPushRoleWrap {
    ::v-deep.ivu-col-span-6 {
        height: 75px;
    }
}
</style>

